package cn.edu.sdjzu.xg.eagle.dao;

import cn.edu.sdjzu.xg.eagle.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public final class UserDaoImpl implements UserDao {
    @Override
    public void login(Connection connection, User user) throws SQLException {
        String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
        PreparedStatement pstmt = connection.prepareStatement(sql);
        pstmt.setString(1, user.getUsername());
        pstmt.setString(2, user.getPassword());
        ResultSet resultSet = pstmt.executeQuery();

        // 如果表中存在匹配的记录，则获取表中的 user_id，并更新 User 对象的 id
        if (resultSet.next()) {
            int userId = resultSet.getInt("id");
            user.setId((long) userId); // 将查询到的 id 写入 User 对象
        }
    }
}